package com.yizisu.basemvvm.mvvm.mvvm_helper

import androidx.lifecycle.*

@Deprecated("未完成")
object LiveDateEventBus {
    private val dataList = mutableMapOf<String, LiveBean<Any?>>()

    fun observer(key: String, owner: LifecycleOwner, observer: Observer<Any?>): LiveBean<Any?> {
        return observerEventBus(key, owner, false, observer)
    }

    fun observerForever(key: String, observer: Observer<Any?>): LiveBean<Any?> {
        return observerEventBus(key, null, true, observer)
    }

    private fun observerEventBus(key: String,
                                 owner: LifecycleOwner?,
                                 isReceiverOnBackground: Boolean = false,
                                 observer: Observer<Any?>): LiveBean<Any?> {
        var exitLiveData: LiveBean<Any?>? = dataList[key]
        if (exitLiveData == null) {
            exitLiveData = LiveBean()
        }
        if (isReceiverOnBackground) {
            exitLiveData.observeForever(observer)
        } else {
            exitLiveData.observe(owner!!, observer)
        }
        return exitLiveData
    }


    fun removeObserver(data: LiveBean<Any?>, observer: Observer<Any?>) {
        data.removeObserver(observer)
    }

    fun post(key: String, event: Any?) {
        dataList[key]?.success(event)
    }
}


